package com.supor.aiot.food.view;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.supor.aiot.food.R;

import java.util.ArrayList;
import java.util.List;

public class ShareDialog extends Dialog {
    public ShareDialog(Context context, int themeResId) {
        super(context, themeResId);
    }
    public static class Params {
        private final List<BottomMenu> menuList = new ArrayList<>();
        private View.OnClickListener cancelListener;
        private View.OnClickListener wechatListener;
        private View.OnClickListener friendListener;
        private CharSequence menuTitle;
        private Context context;
    }
    public static class Builder {
        private boolean canCancel = true;
        private boolean shadow = true;
        private final Params p;
        public Builder(Context context) {
            p = new Params();
            p.context = context;
        }
        public Builder setCanCancel(boolean canCancel) {
            this.canCancel = canCancel;
            return this;
        }
        public Builder setShadow(boolean shadow) {
            this.shadow = shadow;
            return this;
        }
        public Builder setTitle(CharSequence title) {
            this.p.menuTitle = title;
            return this;
        }
        public Builder addMenu(String text, View.OnClickListener listener) {
            BottomMenu bm = new BottomMenu(text, listener);
            this.p.menuList.add(bm);
            return this;
        }
        public Builder addMenu(int textId, View.OnClickListener listener) {
            return addMenu(p.context.getString(textId), listener);
        }
        public Builder setCancelListener(View.OnClickListener cancelListener) {
            p.cancelListener = cancelListener;
            return this;
        }

        public Builder setWechatListener(View.OnClickListener cancelListener) {
            p.wechatListener = cancelListener;
            return this;
        }

        public Builder setFriendListener(View.OnClickListener cancelListener) {
            p.friendListener = cancelListener;
            return this;
        }

        public ShareDialog create() {
            final ShareDialog dialog = new ShareDialog(p.context, shadow ? R.style.Theme_Light_NoTitle_Dialog : R.style.Theme_Light_NoTitle_NoShadow_Dialog);
            Window window = dialog.getWindow();
            window.setWindowAnimations(R.style.Animation_Bottom_Rising);
            window.getDecorView().setPadding(0, 0, 0, 0);
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(lp);
            window.setGravity(Gravity.BOTTOM);
            View view = LayoutInflater.from(p.context).inflate(R.layout.dialog_common_share, null);
            LinearLayout llFriendMatrix = view.findViewById(R.id.ll_friend_matrix);
            LinearLayout llWechat = view.findViewById(R.id.ll_share_wechat);
            TextView tvCancle = view.findViewById(R.id.tv_share_cancle);

            ViewGroup.LayoutParams lpItem = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            ViewGroup.MarginLayoutParams lpDivider = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1);
            lpDivider.setMargins(50,0,50,0);

            llFriendMatrix.setOnClickListener(p.friendListener);

            llWechat.setOnClickListener(p.wechatListener);

            if (p.cancelListener != null) {
                tvCancle.setOnClickListener(p.cancelListener);
            } else {
                tvCancle.setOnClickListener(v -> dialog.dismiss());
            }
            dialog.setContentView(view);
            dialog.setCanceledOnTouchOutside(canCancel);
            dialog.setCancelable(canCancel);
            return dialog;
        }
    }
    private static class BottomMenu {
        public String funName;
        public View.OnClickListener listener;
        public BottomMenu(String funName, View.OnClickListener listener) {
            this.funName = funName;
            this.listener = listener;
        }
    }
}
